package com.han.controller;

import com.alibaba.druid.util.StringUtils;
import com.han.dao.AssistDao;
import com.han.model.GoodsEntity;
import com.han.model.ResultInfo;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2019/4/8.
 * @author han
 * 商品其他辅助设置
 */

@RestController
@RequestMapping("/assist")
public class AssistController {

    @Resource
    public AssistDao assistDao;


    @GetMapping("/listcol")
    public ModelAndView list(Model model,HttpServletRequest request) {

        Integer goods_id = Integer.parseInt(request.getParameter("goods_id"));
        List<Map> allColors = assistDao.getAllColorByGoodId(goods_id);
        model.addAttribute("colorList",allColors);
        model.addAttribute("goods_id", goods_id);
        return new ModelAndView("listcolor","colorModel",model);
    }


    @RequestMapping("/formcolor")
    public ModelAndView  createForm(String goods_id,String color_name) {

        Map map = new HashMap();
        map.put("goods_id",goods_id);
        map.put("color_name",color_name);
        Integer result = assistDao.insertColor(map);

        return new ModelAndView("redirect:/assist/listcol?goods_id="+goods_id);//重定向到list页面;

    }


    @GetMapping("/listpitch")
    public ModelAndView listPitch(Model model,HttpServletRequest request) {

        Integer goods_id = Integer.parseInt(request.getParameter("goods_id"));
        List<Map> allPitches = assistDao.getAllPitchByGoodId(goods_id);
        model.addAttribute("pitchList",allPitches);
        model.addAttribute("goods_id", goods_id);
        return new ModelAndView("listpitch","pitchModel",model);
    }


    @RequestMapping("/formpitch")
    public ModelAndView  createPitchForm(String goods_id,String pitch_name) {

        Map map = new HashMap();
        map.put("goods_id",goods_id);
        map.put("pitch_name",pitch_name);
        Integer result = assistDao.insertPitch(map);

        return new ModelAndView("redirect:/assist/listpitch?goods_id="+goods_id);//重定向到list页面;

    }


    @GetMapping("/listscrew")
    public ModelAndView listScrew(Model model,HttpServletRequest request) {

        Integer goods_id = Integer.parseInt(request.getParameter("goods_id"));
        List<Map> allScrew = assistDao.getAllScrewByGoodId(goods_id);
        model.addAttribute("screwList",allScrew);
        model.addAttribute("goods_id", goods_id);
        return new ModelAndView("listscrew","screwModel",model);
    }


    @RequestMapping("/formscrew")
    public ModelAndView  createScrewForm(String goods_id,String screw_name) {

        Map map = new HashMap();
        map.put("goods_id",goods_id);
        map.put("screw_name",screw_name);
        Integer result = assistDao.insertScrew(map);

        return new ModelAndView("redirect:/assist/listscrew?goods_id="+goods_id);//重定向到list页面;

    }

    @RequestMapping("/image")
    public ModelAndView  addImage(Model model,HttpServletRequest request) {

        Integer goods_id = Integer.parseInt(request.getParameter("goods_id"));
        List<Map> allScrew = assistDao.getAllImageByGoodId(goods_id);
        model.addAttribute("imageList",allScrew);
        model.addAttribute("goods_id", goods_id);
        return new ModelAndView("listimage","imageModel",model);

    }

    @PostMapping("/upload")
    public ModelAndView  uploadImg(String goods_id,String image_type,
                                   @RequestParam("file") MultipartFile[] files)throws Exception {

        ResultInfo resultInfo = new ResultInfo();
        // 文件保存的命名空间
        String fileSpace = "E:/img";
        // 保存到数据库中的相对路径
        String uploadPathDB = "/" + goods_id + "/image";

        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            if (files != null && files.length > 0) {

                String fileName = files[0].getOriginalFilename();
                if (!StringUtils.isEmpty(fileName)) {
                    // 文件上传的最终保存路径
                    String finalFacePath = fileSpace + uploadPathDB + "/" + fileName;
                    // 设置数据库保存的路径
                    uploadPathDB += ("/" + fileName);

                    File outFile = new File(finalFacePath);
                    if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                        // 创建父文件夹
                        outFile.getParentFile().mkdirs();
                    }

                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = files[0].getInputStream();
                    IOUtils.copy(inputStream, fileOutputStream);
                }

            } else {
                resultInfo.setSuccess(false);
                resultInfo.setMassage("上传出错...");

            }
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo.setSuccess(false);
            resultInfo.setMassage("上传出错...");

        } finally {
            if (fileOutputStream != null) {
                fileOutputStream.flush();
                fileOutputStream.close();
            }
        }
        //数据入库
        Map map = new HashMap();
        map.put("goods_id",goods_id);
        map.put("image_type",image_type);
        map.put("image_path",fileSpace+uploadPathDB);

        assistDao.insertImgMsg(map);

        resultInfo.setSuccess(true);
        resultInfo.setMassage("上传成功...");
        return new ModelAndView("redirect:/assist/image?goods_id="+goods_id);//重定向到list页面;;

    }



    @GetMapping("/deletecol")
    public ModelAndView deleteCol(HttpServletRequest request) {

        String goods_id = request.getParameter("goods_id");
        Integer color_id = Integer.parseInt(request.getParameter("color_id"));

        assistDao.deleteColorById(color_id);

        return new ModelAndView("redirect:/assist/listcol?goods_id="+goods_id);
    }

    @GetMapping("/deletepitch")
    public ModelAndView deletePitch(HttpServletRequest request) {

        String goods_id = request.getParameter("goods_id");
        Integer pitch_id = Integer.parseInt(request.getParameter("pitch_id"));

        assistDao.deletePitchById(pitch_id);
        return new ModelAndView("redirect:/assist/listpitch?goods_id="+goods_id);
    }

    @GetMapping("/deletescrew")
    public ModelAndView deleteScrew(HttpServletRequest request) {

        String goods_id = request.getParameter("goods_id");
        Integer screw_id = Integer.parseInt(request.getParameter("screw_id"));

        assistDao.deleteScrewById(screw_id);

        return new ModelAndView("redirect:/assist/listscrew?goods_id="+goods_id);
    }

    @GetMapping("/deleteimg")
    public ModelAndView deleteImg(HttpServletRequest request) {

        String goods_id = request.getParameter("goods_id");
        Integer image_id = Integer.parseInt(request.getParameter("image_id"));

        assistDao.deleteImageById(image_id);

        return new ModelAndView("redirect:/assist/image?goods_id="+goods_id);
    }



}
